#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "config.h"

int main()
{
    int err;
    int s;
    char buff[256] = {'\0'};
    struct sockaddr_in recv_addr;
    int recv_addr_len;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0)
    {
        perror("socket()");
        exit(1);
    }

    struct sockaddr_in addr;
    int addr_len = sizeof(addr);
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(UDP_TEST_ADDR);
    addr.sin_port = htons(UDP_TEST_PORT);

    while (scanf("%s", buff))
    {
        err = sendto(s, buff, strlen(buff), 0, (struct sockaddr *)&addr, addr_len);
        if (err < 0)
        {
            perror("sendto()");
            exit(1);
        }

        err = recvfrom(s, buff, 256, 0, (struct sockaddr *)&addr, &addr_len);
        if (err < 0)
        {
            perror("recvfrom()");
            exit(1);
        }

        printf("%s\n", buff);
    }

    return 0;
}